import Cache from "@/utils/cache";
import {
	getUserinfo
} from "@/http/api/user/user.js"
import {
	getCategoryList
} from "@/http/api/category/category.js"
import {
	getUserShopDetails
} from "@/http/api/shop/shop.js"
// #ifndef VUE3
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const store = new Vuex.Store({
			// #endif

			// #ifdef VUE3
			import {
				createStore
			} from 'vuex'
			const store = createStore({
				// #endif
				state: {
					//登录用户的信息
					userInfo: JSON.parse(Cache.get('userInfo') || JSON.stringify('')),
					userShopDetails: {}, //当前登录店铺信息
					userTutorDetails: {}, //当前登录教练信息
					categoryList: [], //分类列表
					isMerchant: '', //0是商家 1不是
					isAssistant: '', //0是教练 1不是
					addressInfo: {
						latitude: '', // 纬度
						longitude: '', // 经度
						name: '', // 名称
						province: '', // 省份
						city: '', // 市区
						district: '', // 区域
						businessAreaName: '', // 商圈
					}, //选择的定位数据 
					userId: null, //登录用户id
					//登录后跳转的页面路径 + 页面参数
					returnUrl: "",
					//app版本
					version: "3.0.0",
					//当前是否有网络连接
					networkConnected: true,
					isOnline: true,
					whichEnd: 'shop', // index 用户端   shop 商户端  tutor 教练端
					merchantTabBar: [{
							"pagePath": "/pages/shop-control/shop-control",
							"text": "店铺管理",
							"iconPath": "/static/tabbar/shop/shopControl.png",
							"selectedIconPath": "/static/tabbar/shop/shopControl_active.png",
							"num": 0,
							"isDot": false,
							"verify": true
						},
						{
							"pagePath": "/pages/distribution-control/distribution-control",
							"text": "分销管理",
							"iconPath": "/static/tabbar/shop/distributionControl.png",
							"selectedIconPath": "/static/tabbar/shop/shopControl_active.png",
							"num": 0,
							"isDot": true,
							"verify": true
						},
						{
							"pagePath": "/pages/appraise-control/appraise-control",
							"text": "评价管理",
							"iconPath": "/static/tabbar/shop/appraiseControl.png",
							"selectedIconPath": "/static/tabbar/shop/appraiseControl_active.png",
							"num": 0,
							"isDot": true,
							"verify": true
						},
						{
							"pagePath": "/pages/tutor-control/tutor-control",
							"text": "教练管理",
							"iconPath": "/static/tabbar/shop/tutorControl.png",
							"selectedIconPath": "/static/tabbar/shop/tutorControl_active.png",
							"num": 0,
							"isDot": true,
							"verify": true
						},
						// {
						// 	"pagePath": 'pages/mine/mine',
						// 	"text": "个人中心",
						// 	"iconPath": "/static/tabbar/shop/tutorControl.png",
						// 	"selectedIconPath": "/static/tabbar/shop/tutorControl_active.png",
						// 	"num": 0,
						// 	"isDot": true,
						// 	"verify": true
						// }
					],
					tabBarIndex: 0
				},
				mutations: {
					setMerchant(state, val) {
						state.isMerchant = val;
					},
					setUserShopDetails(state, val) {
						console.log(val, '设置商家信息')
						state.userShopDetails = val;
					},
					//设置当前登录教练端信息 
					setUserTutorDetails(state, val) {
						state.userTutorDetails = val;
					},
					//设置分类列表
					setCategoryList(state, val) {
						state.categoryList = val;
					},
					clearAdressInfo(state, val) {
						state.addressInfo = {
							latitude: '', // 纬度
							longitude: '', // 经度
							name: '', // 名称
							province: '', // 省份
							city: '', // 市区
							district: '', // 区域
							businessAreaName: '', // 商圈
						}
					},
					setAssistant(state, val) {
						state.isAssistant = val;
					},
					setAdressInfo(state, val) {
						state.addressInfo = val;
					},
					setUserInfo(state, val) {
						state.userInfo = val;
					},
					changeTabItem(state, item) {
						if (item) {
							state.whichEnd = item
						}
					},
					setTabBarIndex(state, payload) {
						state.tabBarIndex = payload.index
					},
					changeTabBar(state, payload) {
						console.log(payload, '点击tabber')
						state.tabBarIndex = payload.index
					},
					login(state, payload) {
						if (payload) {
							state.mobile = payload.mobile
						}
						state.isLogin = true
					},
					logout(state) {
						state.mobile = ""
						state.isLogin = false
						state.returnUrl = ""
					},
					setReturnUrl(state, returnUrl) {
						state.returnUrl = returnUrl
					},
					networkChange(state, payload) {
						state.networkConnected = payload.isConnected
					},
					setOnline(state, payload) {
						state.isOnline = state.isOnline
					}
				},
				getters: {
					// 根据 whichEnd 动态选择数据
					tabBar: (state, getters) => {
						// 计算并返回tabbar数据
						return state.merchantTabBar;
					},
					// 根据 id 返回分类name
					categoryName: (state) => (id) => {
						const item = state.categoryList.find((item) => item.id === id);
						return item ? item.categoryName : null; // 如果找不到，返回 null
					},
				},
				actions: {
					getUserInfo(store) {
						getUserShopDetails().then((res) => {
							if (res.code == 200) {
								store.commit("setUserShopDetails", res.data);
								Cache.set('userId', res.data && res.data.id)
								let info = JSON.stringify(res.data)
								Cache.set('userInfo', info)
							} else {
								store.commit("setUserInfo", false);
							}
						});
					},
					getCategorys(store) {
						getCategoryList().then((res) => {
							if (res.code == 200) {
								store.commit("setCategoryList", res.data);
							} else {
								store.commit("setUserInfo", false);
							}
						});
					},
				}
			})

			export default store